<?php

class Default_Form_Contact extends Zend_Form {
	
	const NAME_LABEL        = 'Tên';
	const EMAIL_LABEL       = 'Email';
	const TITLE_LABEL      	= 'Tiêu đề';
	const CONTENT_LABEL     = 'Nội dung';
	
	const ENCODING              = 'UTF-8';
	const TITLE_MIN_LENGTH    	= 20;
	const TITLE_MAX_LENGTH    	= 200;
	const NAME_MIN_LENGTH    	= 2;
	const NAME_MAX_LENGTH    	= 50;
	const CONTENT_MIN_LENGTH    = 30;
	const CONTENT_MAX_LENGTH    = 600;
	
	public function init() {
		$this->setName('frmContact');
		
        // Name
        $name = self::addName();
		
        // Email
        $email = self::addEmail();
		
        // Title
        $title = self::addTitle();
		
		// Content
		$content = self::addContent();
		
		// Captcha
		$captcha = self::addCaptcha();
        
		// Button
       	$submit = new Zend_Form_Element_Submit('submit');
        $submit ->setAttrib('id', 'submitbutton')
        		->setAttrib("class", "btn btn-large btn-primary ")
        		->removeDecorator('DtDdWrapper');
        $reset = new Zend_Form_Element_Reset('reset');
        $reset	->setAttrib('id', 'resetbutton')
        		->setAttrib("class", "btn btn-large")
        		->removeDecorator('DtDdWrapper');
		
		$this->addElements(array($name, $email, $title, $content, $captcha, $submit, $reset));
		
		$this->addDisplayGroup(array('submit', 'reset'), 'buttons');
        $dg = $this->getDisplayGroup('buttons');
        $dg->removeDecorator('Fieldset');
        $dg->removeDecorator('HtmlTag');
	}
	
	/*
	 * Add full_name element
	 */
	protected  function addName() {
        $nameNotEmpty = new Zend_Validate_NotEmpty();
        //Length validate
        $nameLength = new Zend_Validate_StringLength(self::NAME_MIN_LENGTH, self::NAME_MAX_LENGTH, self::ENCODING);
        $name = new Zend_Form_Element_Text('ho_ten');
        $name	->setLabel(self::NAME_LABEL)
        			->addFilter('StripTags')
        			->addFilter('StringTrim')
        			->setRequired(true)
        			->addValidator($nameNotEmpty, true)
        			->addValidator($nameLength);
        return $name;
	}
	
	/*
	 * Add email element
	 */
	protected function addEmail() {
		 $tb_name = new Admin_Model_DbTable_Users();
         $email = new Zend_Form_Element_Text('email');
         $emailNotEmpty = new Zend_Validate_NotEmpty();
         $email	->setLabel(self::EMAIL_LABEL)
         		->setRequired(true)
         		->addValidator($emailNotEmpty, true)
         		->addFilter('StripTags')
         		->addFilter('StringTrim')
         		->addValidator(new Zendhlt_Validate_Email());
         return $email;
	}

	/*
	 * Add title element
	 */
	protected function addTitle() {
		$notEmptyTitle = new Zend_Validate_NotEmpty();
		$titleAlpha = new Zend_Validate_Alpha(true);
        //Length validate
        $titleLength = new Zend_Validate_StringLength(self::TITLE_MIN_LENGTH, self::TITLE_MAX_LENGTH, self::ENCODING);
        $title = new Zend_Form_Element_Text('tieu_de');
        $title	->setLabel(self::TITLE_LABEL)
        		->addFilter('StripTags')
        		->addFilter('StringTrim')
        		->setRequired(true)
        		->addValidator($titleLength)
        		->addValidator($titleAlpha)
        		->addValidator($notEmptyTitle, true);
        
        return $title;
	}

	/*
	 * Add content element
	 */
	protected function addContent() {
        //Length validate
        $contentLength = new Zend_Validate_StringLength(self::CONTENT_MIN_LENGTH, self::CONTENT_MAX_LENGTH, self::ENCODING);
        $content = new Zend_Form_Element_Textarea('noi_dung');
        $content->setLabel(self::CONTENT_LABEL)
        		->addFilter('StripTags')
        		->addFilter('StringTrim')
        		->setRequired(true)
        		->addValidator($contentLength);
        
        return $content;
	}

	/*
	 * Add captcha element
	 */
	protected function addCaptcha() {
		
		$captcha = new Zendhlt_Form_Element_Captcha('captcha',array(
				'label' => 'Mã xác nhận',
	 			'captcha' => array(
						'captcha' => 'Image',
						'wordLen' => 6,
						'timeout' => 300,
						'font' => APPLICATION_PATH.'/../public/fonts/League_Gothic_webfont.ttf',
						'imgDir' => APPLICATION_PATH.'/../public/captcha/',
						'imgUrl' => '/public/captcha/',
        				'imgAlt' => 'captcha',
						'height' => 90,
						'width' => 350,
						'fontSize' => 50,
        				'messages' => array('badCaptcha' => 'Mã xác nhận không đúng với ảnh!')
				),
		));
		
		return $captcha;
	}
}